#! /bin/bash
source .env
source ../common.sh

LINE_STR="==============================>>>"
echo "${LINE_STR} commands available to users: "
echo "bash startup.sh: 启动容器"
echo "bash startup.sh start: 启动容器"
echo "bash startup.sh save: 保存镜像"
echo "bash startup.sh load: 加载镜像" 
echo "bash startup.sh config: 打印配置" 

osp_name="skywalking-oap"
ui_name="skywalking-ui"
function start() {
  docker-compose down
  docker-compose up -d
}

# 保存镜像
function imageSave() {
  echo "${LINE_STR} before save image"
  if [ -f "$PWD/${osp_name}.tar.gz" ]; then
    rm -rf ${osp_name}.tar.gz
  fi
  if [ -f "$PWD/${ui_name}.tar.gz" ]; then
    rm -rf ${ui_name}.tar.gz
  fi
  docker save ${SKYWALKING_OAP_IMAGE} | gzip > ${osp_name}.tar.gz
  docker save ${SKYWALKING_UI_IMAGE} | gzip > ${ui_name}.tar.gz
  echo "${LINE_STR} after save image"
}

# 加载镜像
function imageLoad() {
  echo "${LINE_STR} before load image"
  gunzip -c ${osp_name}.tar.gz | docker load
  gunzip -c ${ui_name}.tar.gz | docker load
  echo "${LINE_STR} after load image"
}

echo "input command: $1"
if [ "$1" == "start" ]; then
  start 
elif [ "$1" == "save" ]; then
  imageSave
elif [ "$1" == "load" ]; then
  imageLoad
elif [ "$1" == "config" ]; then
  docker-compose config
else
  start
fi